﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

namespace MINERP.DAL.NewPhotoRindGrid
{
    /// <summary>
    ///  处理分片中与格子相关的数据访问
    /// </summary>
    public class GridDAL_Grid
    {
        public int GetGridInfoBySubAreaId(int subAreaId, out List<Grid_Info> Grids,int isAllGrid)
        {
            int result = 0;//代表成功
            Grids = null;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetGridInfoBySubAreaId");
            db.AddInParameter(dbCommand, "subAreaId", DbType.Int32, subAreaId);
            db.AddInParameter(dbCommand, "isAllGrid", DbType.Int32, isAllGrid);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        if (Grids == null)
                        {
                            Grids = new List<Grid_Info>();
                        }

                        Grids.Add(new Grid_Info() { FirstEnterTime = dr.GetDateTime(3), GridId = dr.GetInt32(0), GridName = dr.GetString(1), SomeId = dr.GetInt32(2), Status = dr.GetInt32(4) });
                    }
                }
            }
            catch (SqlException)
            {
                result = 1;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 分配格子
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <param name="gridId"></param>
        /// <returns></returns>
        public int AssignGridForOrderProductId(int orderProductId, int subAreaTypeId, int curSubAreaId, out bool isNewGrid, out Grid_Info gInfo)
        {
            int result = 1;//代表成功 
            isNewGrid = false;
            gInfo = null;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("AssignGridForProduct");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);
            db.AddInParameter(dbCommand, "subAreaTypeId", DbType.Int32, subAreaTypeId);
            db.AddInParameter(dbCommand, "curSubAreaId", DbType.Int32, curSubAreaId);
            db.AddOutParameter(dbCommand, "isNewGrid", DbType.Int16, 1);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        gInfo = new Grid_Info() { FirstEnterTime = dr.GetDateTime(0), GridId = dr.GetInt32(1), GridName = dr.GetString(2), Status = dr.GetInt32(3), SomeId = orderProductId };
                    }
                }
                isNewGrid = Convert.ToBoolean(db.GetParameterValue(dbCommand, "isNewGrid"));
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="photoId">要分片的照片ID</param>
        /// <param name="subAreaTypeId">照片分区的类型ID</param>
        /// <param name="curSubAreaId">当前分区ID</param>
        /// <param name="isNewGrid">是否为新格子</param>
        /// <param name="isPrint">是否需要打印,2表示重打、0表示不打印，1-表示正常的分配完成打印</param>
        /// <param name="strNextTechName">下一个流程结点NAME</param>
        /// <param name="GridId">分配的格子ID</param>
        /// <param name="opUid">操作员</param>
        /// <param name="curTechId">当前流程ID</param>
        /// <returns></returns>
        public int AssignGridForPhoto(int photoId, int subAreaTypeId, int curSubAreaId, out bool isNewGrid, out int isPrint, out string strNextTechName, out int GridId, int opUid, int curTechId, out int orderProductId, out string shopGridName, bool isAutoAssignShop, out string outProductOrderInBag,out string outShopName)
        {
            int result = 1;//代表成功 

            isNewGrid = false;
            isPrint = 0;
            strNextTechName = string.Empty;
            GridId = 0;
            orderProductId = 0;
            shopGridName = string.Empty;
            outProductOrderInBag = string.Empty;
            outShopName = string.Empty;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("AssignGridForPhoto");
            db.AddInParameter(dbCommand, "photoId", DbType.Int32, photoId);
            db.AddInParameter(dbCommand, "subAreaTypeId", DbType.Int32, subAreaTypeId);
            db.AddInParameter(dbCommand, "curSubAreaId", DbType.Int32, curSubAreaId);
            db.AddInParameter(dbCommand, "OpUid", DbType.Int32, opUid);
            db.AddInParameter(dbCommand, "techId", DbType.Int32, curTechId);
            db.AddInParameter(dbCommand, "isAutoAssignShop", DbType.Boolean, isAutoAssignShop);


            db.AddOutParameter(dbCommand, "isNewGrid", DbType.Int16, 1);
            db.AddOutParameter(dbCommand, "isPrint", DbType.Int32, 1);
            db.AddOutParameter(dbCommand, "nextTechName", DbType.String, 100);
            db.AddOutParameter(dbCommand, "photoGridId", DbType.Int32, 4);
            db.AddOutParameter(dbCommand, "photoIsExists", DbType.Int16, 1);
            db.AddOutParameter(dbCommand, "orderProductId", DbType.Int32, 4);
            db.AddOutParameter(dbCommand, "shopGridName",DbType.String,100);
            db.AddOutParameter(dbCommand, "isSuss", DbType.Int16, 1);
            db.AddOutParameter(dbCommand, "outPutProductOrderInBag", DbType.String, 50);
            db.AddOutParameter(dbCommand, "outShopName", DbType.String, 50);

            try
            {
                db.ExecuteNonQuery(dbCommand);

                isNewGrid = Convert.ToBoolean(db.GetParameterValue(dbCommand, "isNewGrid"));

                if (!Convert.ToBoolean(db.GetParameterValue(dbCommand, "photoIsExists")))
                {
                    return -1;//照片不存在
                }
                result = Convert.ToInt32(db.GetParameterValue(dbCommand, "isSuss"));
                if (result==0)
                {
                    if (isNewGrid)
                    {
                        return -5;
                    }
                    return -2;//执行操作失败
                }

                isPrint = Convert.ToInt32(db.GetParameterValue(dbCommand, "isPrint"));
                strNextTechName = Convert.ToString(db.GetParameterValue(dbCommand, "nextTechName"));
                GridId = Convert.ToInt32(db.GetParameterValue(dbCommand, "photoGridId"));
                orderProductId = Convert.ToInt32(db.GetParameterValue(dbCommand, "orderProductId"));
                shopGridName = Convert.ToString(db.GetParameterValue(dbCommand, "shopGridName"));
                outProductOrderInBag = Convert.ToString(db.GetParameterValue(dbCommand, "outPutProductOrderInBag")).Trim();
                outShopName = Convert.ToString(db.GetParameterValue(dbCommand, "outShopName")).Trim();
                //result = Convert.ToInt32(db.GetParameterValue(dbCommand, "isSuss"));
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 清空格子
        /// </summary>
        /// <param name="gridId"></param>
        /// <returns></returns>
        public int ClearGrid(int gridId)
        {
            int result = 0;//代表成功

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("clearGrid");
            db.AddInParameter(dbCommand, "gridId", DbType.Int32, gridId);
            try
            {
                db.ExecuteNonQuery(dbCommand);
                result = 1;
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 强制打印一个格子用来完成流程用
        /// </summary>
        /// <param name="opUid"></param>
        /// <param name="techId"></param>
        /// <param name="orderProdutId"></param>
        /// <param name="strNextTechName"></param>
        /// <returns></returns>
        public int OverTechForForcePrintGrid(int opUid, int techId, int orderProdutId, out string strNextTechName, out string shopGridName, out string outProductOrderInBag)
        {
            int result = 0;//代表成功
            strNextTechName = string.Empty;
            shopGridName = string.Empty;
            outProductOrderInBag = string.Empty;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OverCurTechForGrid");


            db.AddInParameter(dbCommand, "OpUid", DbType.Int32, opUid);
            db.AddInParameter(dbCommand, "techId", DbType.Int32, techId);
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProdutId);

            db.AddOutParameter(dbCommand, "nextTechName", DbType.String, 100);
            db.AddOutParameter(dbCommand, "shopGridName", DbType.String, 100);
            db.AddOutParameter(dbCommand, "isSuss", DbType.Int16, 1);
            db.AddOutParameter(dbCommand, "outPutProductOrderInBag", DbType.String, 50);
            try
            {
                db.ExecuteNonQuery(dbCommand);
                result = Convert.ToInt32(db.GetParameterValue(dbCommand, "isSuss"));
                if (result==0)
                {
                    return -2;//执行操作失败
                }
                shopGridName = db.GetParameterValue(dbCommand, "shopGridName").ToString();
                strNextTechName = db.GetParameterValue(dbCommand, "nextTechName").ToString();
                outProductOrderInBag = db.GetParameterValue(dbCommand, "outPutProductOrderInBag").ToString().Trim();
                result = 1;
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 得到格子本来的基本信息
        /// </summary>
        /// <param name="gridId"></param>
        /// <returns></returns>
        public Grid_Info GetGridViewInfoByGridId(int gridId)
        {
            int result = 0;//代表成功
            Grid_Info Grid = null;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetGridBaseInfoByGridId");
            db.AddInParameter(dbCommand, "gridid", DbType.Int32, gridId);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        Grid = new Grid_Info() { FirstEnterTime = dr.GetDateTime(3), GridId = dr.GetInt32(0), GridName = dr.GetString(1), SomeId = dr.GetInt32(2), Status = dr.GetInt32(4) };
                    }
                }
            }
            catch (SqlException)
            {
                result = 1;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return Grid;
        }


        public int PriorityGrid(List<int> orderProductIdList)
        {
            int result = 1;//代表成功
            Database db = DatabaseFactory.CreateDatabase();

            using (DbConnection con = db.CreateConnection())
            {
                try
                {
                    DbCommand dbCommand = db.GetStoredProcCommand("PriorityOrderByOrderProductId");
                    dbCommand.Connection = con;
                    con.Open();
    
                    foreach (int orderProductId in orderProductIdList)
                    {
                        if (dbCommand.Parameters.Count>0)
                        {
                            dbCommand.Parameters.Clear();
                        }                    

                        db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);

                        db.ExecuteNonQuery(dbCommand);
                    }
                }
                catch (Exception)
                {
                    result = -2;
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
            return result;

        }

        //public string GetSubGridNameByGridId(int inGridId)
        //{
        //    string GridName="";
        //    try
        //    {
        //        Database db = DatabaseFactory.CreateDatabase();
        //        string strSql = "select SubAreaName from Grid,GridSubArea where Grid.SubAreaID=GridSubArea.SubAreaID and GridId=" + inGridId;
               
        //        DbCommand dbCommand = db.GetSqlStringCommand(strSql);
        //        dbCommand.Connection=db.c
        //        GridName = Convert.ToString(dbCommand.ExecuteScalar());
        //    }
        //    catch (Exception exx)
        //    {
        //        throw exx;
        //    }
        //    return GridName;
        //}

    }
}
